import regeneratorRuntime from '../../resource/libs/runtime'
import { asyncReverseGeocoder, asyncGetSuggestion } from '../../common/utils'

let app = getApp()

Page({
  data: {
    type: '', // 选择地址的类型
    region: [], // 省市区
    list: [], // 可选地址列表，组装成二维数组，每一页为一项，防止一次性渲染太多数据
    if_loading: false,
    if_no_more: false,
    page_index: 0, // 当前页码，初始值为 0
    keyword: '', // 搜索关键词
    input_focus: false,
  },

  async onLoad ({ type }) {
    let { o_region, o_location, d_region, d_location } = app.global_data
    console.log(app.global_data)
    let region = o_region
    let location = o_location
    if (type === 'destination' && d_region.length > 0) {
      region = d_region
      location = d_location
    }
    console.log(type, region, location)
    this.setData({ type, region, if_loading: true })
    let res = await asyncReverseGeocoder({ location, get_poi: 1 })
    this.setData({ if_loading: false })
    if (res) {
      this.setData({ 'list[0]': res.result.pois, if_no_more: true })
    }
  },

  regionChange ({ detail }) {
    let region = detail.value
    let old_region = this.data.region
    for (let i = 0; i < 3; i++) {
      if (region[i] !== old_region[i]) {
        this.setData({ region }, () => {
          this.search({
            detail: {
              value: this.data.keyword
            }
          })
        })
        break
      }
    }
  },

  search ({ detail }) {
    this.setData({
      keyword: detail.value,
      list: [],
      if_no_more: false,
      page_index: 0
    }, () => {
      this.getList()
    })
  },

  async getList () {
    if (this.data.if_no_more) {
      return
    }
    let keyword = this.data.keyword
    if (keyword === '') {
      wx.showToast({
        title: '请输入地点搜索',
        icon: 'none'
      })
      this.setData({ input_focus: true })
      return
    }
    let page_index = this.data.page_index
    this.setData({ if_loading: true })
    let res = await asyncGetSuggestion({
      keyword,
      region: this.data.region[1],
      region_fix: 1,
      page_index: page_index + 1
    })
    this.setData({ if_loading: false })
    if (res) {
      let { data } = res
      this.setData({
        ['list[' + page_index + ']']: data,
        if_no_more: data.length ? false : true,
        page_index: page_index + 1
      })
    }
  },

  tapSite ({ currentTarget }) {
    let type = this.data.type
    let { page, index } = currentTarget.dataset
    let item = this.data.list[page][index]
    let { title, ad_info = {}, location } = item
    let region = [
      ad_info.province || item.province,
      ad_info.city || item.city,
      ad_info.district || item.district
    ]
    location = {
      latitude: location.lat,
      longitude: location.lng
    }
    if (type === 'origin') {
      app.updateGlobalData({
        origin: title,
        o_region: region,
        o_location: location
      })
    } else {
      app.updateGlobalData({
        destination: title,
        d_region: region,
        d_location: location
      })
    }
    wx.navigateBack({ delta: 1 })
  }
})
